package com.hz.ruoyiequipment.data.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hz.ruoyiequipment.data.domain.AgrEquipment;
import com.hz.ruoyiequipment.data.domain.AgrEquipmentCq;
import com.hz.ruoyiequipment.data.domain.AgrEquipmentQx;
import com.hz.ruoyiequipment.data.domain.AgrEquipmentTr;
import com.hz.ruoyiequipment.data.domain.vo.*;
import com.hz.ruoyiequipment.data.mapper.AgrEquipmentCqMapper;
import com.hz.ruoyiequipment.data.mapper.AgrEquipmentMapper;
import com.hz.ruoyiequipment.data.mapper.AgrEquipmentQxMapper;
import com.hz.ruoyiequipment.data.mapper.AgrEquipmentTrMapper;
import com.hz.ruoyiequipment.data.service.AgrEquipmentService;
import com.ruoyi.common.core.web.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: xqd
 * @CreateDate: 2025/03/06/10:45
 * @Description: 设备表服务实现类
 */
@Service
public class AgrEquipmentServiceImpl extends ServiceImpl<AgrEquipmentMapper, SoilEquipment> implements AgrEquipmentService {
    @Autowired
    private AgrEquipmentMapper agrEquipmentMapper;
    @Autowired
    private AgrEquipmentTrMapper agrEquipmentTrMapper;
    @Autowired
    private AgrEquipmentQxMapper agrEquipmentQxMapper;
    @Autowired
    private AgrEquipmentCqMapper agrEquipmentCqMapper;


    /**
     * 查询设备列表
     * @Author: zjk
     * @param agrEquipment 设备
     * @return 设备集合
     */
    public List<AgrEquipment> selectAgrEquipmentList(AgrEquipment agrEquipment) {
        return agrEquipmentMapper.selectAgrEquipmentList(agrEquipment);
    }



    /**
     * 查询设备列表
     * @Author: zjk
     * @param equipmentTypes 设备
     * @return 根据设备types查询集合
     */
    @Override
    public List<EquipmentWarningSelectVo> selectAgrEquipmentListByTypes(String[] equipmentTypes) {
        return agrEquipmentMapper.selectAgrEquipmentListByTypes(equipmentTypes);
    }

    @Override
    public List<SoilEquipment> getSoilEquipment(Integer equipmentStatus, String equipmentCode, String plotsName) {
        return agrEquipmentMapper.getSoilEquipment(equipmentStatus, equipmentCode, plotsName);
    }

    @Override
    public List<QxEquipment> getQxEquipment(QxEquipment qxEquipment) {
        return agrEquipmentMapper.getQxEquipment(qxEquipment);
    }

    @Override
    public List<CqEquipment> getCqEquipment(CqEquipment cqEquipment) {
        return agrEquipmentMapper.getCqEquipment(cqEquipment);
    }

    @Override
    public AjaxResult getSoilDetail(Long equipmentId) {
        return AjaxResult.success(agrEquipmentMapper.getSoilDetail(equipmentId));
    }

    @Override
    public AjaxResult getQxDetail(Long equipmentId) {
        return AjaxResult.success(agrEquipmentMapper.getQxDetail(equipmentId));
    }

    @Override
    public AjaxResult getCqDetail(Long equipmentId) {
        return AjaxResult.success(agrEquipmentMapper.getCqDetail(equipmentId));
    }

    @Override
    @Transactional
    public AjaxResult insertSoil(AgrEquipment agrEquipment, AgrEquipmentTr agrEquipmentTr) {
        if (agrEquipment == null || agrEquipmentTr == null) {
            return AjaxResult.error("设备信息或传感器信息为空");
        }
        int rows = agrEquipmentMapper.insertAgrEquipment(agrEquipment);
        if (rows <= 0) {
            return AjaxResult.error("设备信息插入失败");
        }
        long equipmentId = agrEquipment.getEquipmentId();
        agrEquipmentTr.setEquipmentId(equipmentId);
        rows = agrEquipmentTrMapper.insertAgrEquipmentTr(agrEquipmentTr);
        if (rows <= 0) {
            return AjaxResult.error("传感器信息插入失败");
        }
        return AjaxResult.success();
    }

    @Override
    @Transactional
    public AjaxResult insertQx(AgrEquipment agrEquipment, AgrEquipmentQx agrEquipmentQx) {
        if (agrEquipment == null || agrEquipmentQx == null) {
            return AjaxResult.error("设备信息或传感器信息为空");
        }
        int rows = agrEquipmentMapper.insertAgrEquipment(agrEquipment);
        if (rows <= 0) {
            return AjaxResult.error("设备信息插入失败");
        }
        long equipmentId = agrEquipment.getEquipmentId();
        agrEquipmentQx.setEquipmentId(equipmentId);
        rows = agrEquipmentQxMapper.insertAgrEquipmentQx(agrEquipmentQx);
        if (rows <= 0) {
            return AjaxResult.error("传感器信息插入失败");
        }
        return AjaxResult.success();
    }

    @Override
    @Transactional
    public AjaxResult insertCq(AgrEquipment agrEquipment, AgrEquipmentCq agrEquipmentCq ) {
        if (agrEquipment == null || agrEquipmentCq == null) {
            return AjaxResult.error("设备信息或传感器信息为空");
        }
        int rows = agrEquipmentMapper.insertAgrEquipment(agrEquipment);
        if (rows <= 0) {
            return AjaxResult.error("设备信息插入失败");
        }
        long equipmentId = agrEquipment.getEquipmentId();
        agrEquipmentCq.setEquipmentId(equipmentId);
        rows = agrEquipmentCqMapper.insertAgrEquipmentCq(agrEquipmentCq);
        if (rows <= 0) {
            return AjaxResult.error("传感器信息插入失败");
        }
        return AjaxResult.success();
    }

    @Override
    public Integer countJk() {
        return agrEquipmentMapper.countJk();
    }

    @Override
    public Integer countQx() {
        return agrEquipmentMapper.countQx();
    }

    @Override
    public Integer countTr() {
        return agrEquipmentMapper.countTr();
    }

    @Override
    public Integer countSf() {
        return agrEquipmentMapper.countSf();
    }

    @Override
    public Integer countCq() {
        return agrEquipmentMapper.countCq();
    }

    @Override
    public FirstPageEqVo getFirstPageEq() {
        FirstPageEqVo firstPageEqVo = new FirstPageEqVo();
        firstPageEqVo.setCountJk(countJk());
        firstPageEqVo.setCountQx(countQx());
        firstPageEqVo.setCountTr(countTr());
        firstPageEqVo.setCountSf(countSf());
        firstPageEqVo.setCountCq(countCq());
        return firstPageEqVo;
    }
}
